const axios = require('axios');

async function testActivityFunctions() {
  try {
    console.log('测试活动功能...');
    
    // 1. 测试获取活动列表
    console.log('1. 测试获取活动列表...');
    const response = await axios.get('http://localhost:3000/api/activities');
    console.log('✓ 活动列表获取成功，共', response.data.activities.length, '个活动');
    
    if (response.data.activities.length > 0) {
      const activityId = response.data.activities[0].id;
      console.log('使用活动ID:', activityId);
      
      // 2. 测试获取活动详情
      console.log('2. 测试获取活动详情...');
      const detailResponse = await axios.get(`http://localhost:3000/api/activities/${activityId}`);
      console.log('✓ 活动详情获取成功:', detailResponse.data.activity.title);
      console.log('  活动描述:', detailResponse.data.activity.description.substring(0, 50) + '...');
      console.log('  活动地点:', detailResponse.data.activity.location);
      console.log('  参与人数:', detailResponse.data.activity.current_participants + '/' + detailResponse.data.activity.max_participants);
      
      // 3. 测试获取参与者列表
      console.log('3. 测试获取参与者列表...');
      const participantsResponse = await axios.get(`http://localhost:3000/api/activities/${activityId}/participants`);
      console.log('✓ 参与者列表获取成功，共', participantsResponse.data.participants.length, '人');
      
      if (participantsResponse.data.participants.length > 0) {
        console.log('  参与者:', participantsResponse.data.participants.map(p => p.user.username).join(', '));
      }
    } else {
      console.log('⚠️ 没有活动数据，请先运行种子数据脚本');
    }
    
    // 4. 测试搜索功能
    console.log('4. 测试活动搜索功能...');
    const searchResponse = await axios.get('http://localhost:3000/api/activities?search=比赛');
    console.log('✓ 搜索"比赛"找到', searchResponse.data.activities.length, '个活动');
    
    // 5. 测试分类筛选
    console.log('5. 测试分类筛选功能...');
    const categoryResponse = await axios.get('http://localhost:3000/api/activities?category_id=2');
    console.log('✓ 分类筛选找到', categoryResponse.data.activities.length, '个活动');
    
    console.log('🎉 活动功能测试完成！所有API正常工作');
    
  } catch (error) {
    console.error('❌ 测试失败:');
    if (error.response) {
      console.error('状态码:', error.response.status);
      console.error('响应数据:', error.response.data);
    } else {
      console.error('错误信息:', error.message);
    }
  }
}

// 执行测试
testActivityFunctions(); 